using System;
using System.Collections.Generic;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using Barbar.MapReport.Common;
using Barbar.MapReport.Common.Drawing;
using Barbar.MapReport.Common.Drawing.Commands;
using Barbar.MapReport.Common.Drawing.ValueIndicators;
using Barbar.MapReport.Configuration;
using Barbar.MapReport.DataSource.Facade;
using Barbar.MapReport.HtmlRender;
using System.Collections.ObjectModel;
using System.Web;
using System.Xml.Serialization;
using System.Xml;
using System.Text;
using System.IO;

public partial class ResponseDistributionReport : MapReportDemoPage {
  protected void Page_Load(object sender, EventArgs e) {
  }

  protected override void OnPreRender(EventArgs e) {
    if (!Page.IsPostBack) {
      ddlResponses.DataTextField = "Name";
      ddlResponses.DataValueField = "BitFlag";
      ddlResponses.DataSource = ResponseType.Responses;
      ddlResponses.DataBind();

      ddlResponses.Items.Insert(0, new ListItem(">> not specified <<", Convert.ToString(ResponseType.ContactLater.BitFlag + ResponseType.Interested.BitFlag + ResponseType.NoResponse.BitFlag + ResponseType.NotInterested.BitFlag)));
      ddlResponses.Items.Insert(1, new ListItem(">> any response <<", Convert.ToString(ResponseType.ContactLater.BitFlag + ResponseType.Interested.BitFlag + ResponseType.NotInterested.BitFlag)));

      legend.DataSource = ResponseType.Responses;
      legend.DataBind();
    }

    GeoPoint mapCenter = new GeoPoint(51, 11);
    int zoomLevel = 6;
    if (!String.IsNullOrEmpty(Request["regionId"])) {
      RegionRequestById request = new RegionRequestById();
      request.RegionId = Request["regionId"];
      request.IncludeGeoBound = true;

      IDataSourceService service = new DataSourceService();
      SingleRegionResponse response = (SingleRegionResponse)service.GetRegions(request);

      GeoBound bound = response.GeoBound;
      mapCenter = bound.Center;
      zoomLevel = 9;
    }

    Collection<BusinessGeoPoint> points = new Collection<BusinessGeoPoint>();
    foreach (Contact contact in DataProvider.Instance.GetContactByResponseFlag(Convert.ToInt32(ddlResponses.SelectedValue))) {
      if (contact.Latitude.HasValue && contact.Longitude.HasValue) {
        BusinessGeoPoint valuePoint = new BusinessGeoPoint();
        valuePoint.Latitude = contact.Latitude.Value;
        valuePoint.Longitude = contact.Longitude.Value;
        valuePoint.Value1 = contact.ResponseType.Index;
        points.Add(valuePoint);
      }
    }

    AddDynamicValueIndicatorsCommand cmd = new AddDynamicValueIndicatorsCommand();
    GroupProperties properties = new GroupProperties();
    properties.GroupColor = new SerializedColor();
    properties.GroupColor.Color = Color.Yellow;
    properties.ToolTipTemplate = "<span class=\"fieldBold\">Response summary:</span>";
    properties.Groups = new List<Group>();
    cmd.Properties = properties;
    cmd.Points = points;
    foreach (ResponseType responseType in ResponseType.Responses) {
      Group group = new Group();
      group.Color = new SerializedColor();
      group.Color.Color = responseType.Color;
      group.Index = responseType.Index;
      group.Name = responseType.Name;
      properties.Groups.Add(group);
    }
    cmd.Radius = 10;

    RenderingService renderService = new RenderingService();
    renderService.Execute(cmd);

    RenderSettings settings = new RenderSettings(new SerializedUnit(800), new SerializedUnit(600), mapCenter.Latitude, mapCenter.Longitude, zoomLevel, MapConfigurationManager.MapSettings.GoogleKeyProvider.DefaultGoogleKey, 2057);
    settings.EnableGoogleBar = true;

    MapHtmlFragment fragment = renderService.Render(settings);
    litMapDiv.Text = fragment.MapElement;
  }
}
